<template>
  <div>
    <box t1="商品管理" t2="商品列表">
      <!-- 搜索 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable v-model="queryList.query" @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="$router.push('/goods/add')">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表区 -->
      <el-table :data="goodsList" border="" stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="150px" align="center"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100px" align="center"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="200px" align="center">
          <template slot-scope="scope">{{scope.row.add_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="140px" align="center">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeGoodsById(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 页码区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryList.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryList.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background=""
      ></el-pagination>
    </box>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryList: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [],
      total: 0
    };
  },
  methods: {
    async getGoodsList() {
      const res = await this.$http.get("goods", { params: this.queryList });
      if (res.meta.status != 200)
        return this.$message.error("获取商品列表失败");
      this.goodsList = res.data.goods;
      this.total = res.data.total;
      console.log(this.goodsList);
    },
    handleSizeChange(pagesize) {
      this.queryList.pagesize = pagesize;
      this.getGoodsList();
    },
    handleCurrentChange(pagenum) {
      this.queryList.pagenum = pagenum;
      this.getGoodsList();
    },
    async removeGoodsById(id) {
      const isConfirm = await this.$confirm(
        "此操作将永久删除这一商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
        // 点击取消时，通过catch获取取消信息
      ).catch(err => err);
      if (isConfirm == "cancel") return this.$message.info("操作已取消");
      const res = await this.$http.delete("goods/" + id);
      if (res.meta.status != 200) return this.$message.error("删除商品失败");
      this.$message.success("删除商品成功");
      this.getGoodsList();
    }
  },
  created() {
    this.getGoodsList();
  }
};
</script>

<style lang="less" scoped>
.el-table {
  min-width: 1366px;
}
</style>